package com.czd.support.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.model.ZcAnswers;
import com.czd.support.model.ZcQuestion;
import com.czd.support.service.AnswerQuestionService;
import com.czd.support.service.ZcAnswersQuestionService;
import com.czd.support.service.ZcAnswersService;
import com.czd.support.service.ZcQuestionService;

@Controller
@RequestMapping("zcQuestion")
public class ZcQuestionController  extends  BaseController {
	@Autowired
	private ZcQuestionService  zcQuestionService  ;
	@Autowired
	private ZcAnswersQuestionService  zcAnswersQuestionService  ;
	@Autowired
	private ZcAnswersService zcAnswersService ;
	@Autowired
	private AnswerQuestionService  answerQuestionService;
	
	@RequestMapping(value = { "", "/", "/list" })
	public String list(Model model, ZcQuestion zcQuestion,@RequestParam(value = "pageNo", defaultValue = "1") int pageNo) {
		zcQuestion.setPageNo(pageNo);  
		Paginate queryForPaginate = zcQuestionService.queryForPaginate(zcQuestion);
		model.addAttribute("paginate", queryForPaginate);
		return "zcQuestion/list";
	}
	
	@RequestMapping(value = "/{id}/{isAnswer}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id,@PathVariable int isAnswer, Model model){
	    /**删除问题    三张表 */
		//删除问题表
		zcQuestionService.deleteById(id);   
		if(isAnswer==0){
			//未回答
			return "redirect:/zcQuestion/list";
		}else{
			zcAnswersService.deleteByqId(id);
			zcAnswersQuestionService.deleteByqId(id);   
			//判断是否已经回答 .未回答就可以  结束
			//已经回答 则删除其他的表
			return "redirect:/zcQuestion/list";
		}
	}
	/**
	 * 问题答案明细  查看
	 * @param id
	 * @param isAnswer
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(@PathVariable int id , Model model){
		 //根据id  查出2表的数据
	ZcQuestion zcQuestion = zcQuestionService.findById(id);
	ZcAnswers zcAnswers = zcAnswersService.findByqId(id);
	model.addAttribute("zcQuestion", zcQuestion)  ;
	model.addAttribute("zcAnswers", zcAnswers)  ;
		return "zcQuestion/details";
	}
	
	
	
	

}
